package wifiunion.com.notice.widget;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;


import java.util.ArrayList;
import java.util.HashMap;

import wifiunion.com.notice.BasePresenter;
import wifiunion.com.notice.CommonUtils;
import wifiunion.com.notice.Constants;
import wifiunion.com.notice.MyNoticeListener;
import wifiunion.com.notice.NoticeContact;
import wifiunion.com.notice.R;
import wifiunion.com.notice.RecyclerItemClick;
import wifiunion.com.notice.adapter.AddNoticeGroupAdapter;
import wifiunion.com.notice.entity.NoticeGroup;
import wifiunion.com.notice.presenter.AddNoticeGroupPresenter;
import wifiunion.com.notice.presenter.AddNoticeRelGroupPresenter;
import wifiunion.com.notice.presenter.NoticeGroupListPresenter;
import wifiunion.com.notice.utils.SharedPreferencesUtils;

/**
 * Created by Administrator on 2016/12/5.
 * <p>
 * 设备选择相关的对话框
 */

public class AddNoticeGroupDialog extends Dialog implements RecyclerItemClick,NoticeContact.View {
    private Context context;
    private TextView titleName;

    private AddNoticeGroupAdapter mAdapter;

    private ArrayList<NoticeGroup> mList = new ArrayList<NoticeGroup>();

    private  ArrayList<NoticeGroup> mChoooseGooupList = new ArrayList<NoticeGroup>();

    private TextView cancelTxt;
    private TextView confirmTxt;

    private TextView saveTxt;
    private TextView backTxt;


    private RecyclerView mRecyclerView;



    private boolean isSingleSelect = false;

    private TextView addTitle;

    private EditText groupNameEt;

    private int model;//0选择 1添加

    private RelativeLayout choosecontentRl;
    private RelativeLayout addcontentRl;

    private Activity mActivity;

    public MyNoticeListener myListener;

    private NoticeGroupListPresenter mPresenter;
    private AddNoticeGroupPresenter mAddNoticeGroupPresenter;
    private AddNoticeRelGroupPresenter mAddNoticeRelGroupPresenter;
    private String noticeUuid;
    private int pageType;
    private String gUuid;

    //0通知 1巡查
    private int mType;

    public AddNoticeGroupDialog(Context context, Activity activity, int pageType, String noticeUuid, String  gUuid, MyNoticeListener myListener, int type) {
        super(context, R.style.alert_dialog);
        this.context = context;
        this.mActivity = activity;
        this.noticeUuid = noticeUuid;
        this.gUuid = gUuid;
        this.myListener = myListener;
        this.pageType = pageType;
        this.mType = type;
        mAddNoticeRelGroupPresenter = new AddNoticeRelGroupPresenter(getContext(),this);
        mAddNoticeGroupPresenter = new AddNoticeGroupPresenter(getContext(),this);
        mPresenter = new NoticeGroupListPresenter(getContext(), this);
        LayoutInflater li = LayoutInflater.from(context);
        ViewGroup contentView = (ViewGroup) li.inflate(
                R.layout.dialog_addnoticegroup, null);
        contentView.bringToFront();


        cancelTxt = (TextView) contentView.findViewById(R.id.cancel_txt);
        confirmTxt = (TextView) contentView.findViewById(R.id.confirm_txt);

        saveTxt = (TextView) contentView.findViewById(R.id.save_txt);
        backTxt = (TextView) contentView.findViewById(R.id.back_txt);

        titleName = (TextView) contentView.findViewById(R.id.tv_title_name);
        groupNameEt = (EditText) contentView.findViewById(R.id.tv_groupname_message);

        choosecontentRl = (RelativeLayout) contentView.findViewById(R.id.choose_model_rl);
        addcontentRl = (RelativeLayout) contentView.findViewById(R.id.add_model_rl);


        addTitle = (TextView) contentView.findViewById(R.id.tv_title_add);
        addTitle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                model = 1;
                choosecontentRl.setVisibility(View.GONE);
                addcontentRl.setVisibility(View.VISIBLE);
                addTitle.setVisibility(View.GONE);
                titleName.setText("创建分组");

            }
        });

        mRecyclerView = (RecyclerView) contentView.findViewById(R.id.deviceselect_recyclerview);


        mAdapter = new AddNoticeGroupAdapter(getContext(), mList, this);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(context));

        mRecyclerView.setAdapter(mAdapter);


        mAdapter.setOnclickListener(this);


        setCancelListener();
        setComfirmListener();

        setSaveListener();
        setBackListener();
        getGroupList();
        setCanceledOnTouchOutside(true);
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.gravity = Gravity.CENTER;
        onWindowAttributesChanged(lp);
        setContentView(contentView);


    }


    public void setIsSingleSelect(boolean flag) {
        isSingleSelect = flag;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.width = getContext().getResources().getDimensionPixelSize(
                R.dimen.dp_480);
        lp.gravity = Gravity.CENTER;
        lp.height = (int) (CommonUtils.getScreenHeight(getContext()) * 0.9f);
        getWindow().setAttributes(lp);
    }

    public void setTitleMsg(String msg) {
        if (titleName != null) {
            titleName.setText(msg);
        }
    }

    public void setCancleText(String text) {
        if (text != null) {
            cancelTxt.setText(text);
        }
    }


    public void setComfirmListener() {
        if (confirmTxt != null) {
            confirmTxt.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    mChoooseGooupList.clear();
                    for(int i=0;i<mList.size();i++)
                    {
                        if(mList.get(i).getIsSelected()  == 1)
                        {
                            mChoooseGooupList.add(mList.get(i));

                        }
                    }
//                   if(null !=mChoooseGooupList && mChoooseGooupList.size() >0)
//                   {

                       addRelGroup();

//                   }
//                    else
//                   {
//                       //走跳过的处理流程
//                       myListener.refreshData("");
//                       dismiss();
//
//                   }




                }

            });
        }
    }

    public void setCancelListener() {
        if (cancelTxt != null) {
            cancelTxt.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                      myListener.refreshData("");
                        dismiss();
                }
            });
        }
    }

    public void setSaveListener() {
        if (saveTxt != null) {
            saveTxt.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                     saveGroup();
                }
            });
        }
    }

    public void setBackListener() {
        if (backTxt != null) {
            backTxt.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    model = 0;
                    titleName.setText("选择分组");
                    addTitle.setVisibility(View.VISIBLE);
                    choosecontentRl.setVisibility(View.VISIBLE);
                    addcontentRl.setVisibility(View.GONE);


                }
            });
        }
    }



    private int mPos = -1;

    @Override
    public void onItemClick(View view, int postion) {


        mPos = postion;

        if (isSingleSelect) {
            for (int i = 0; i < mList.size(); i++) {
                if (postion == i) {
                    mList.get(i).setIsSelected(1);
                } else {
                    mList.get(i).setIsSelected(0);
                }
            }
        } else {
            if (mList.get(postion).getIsSelected() ==1) {
                mList.get(postion).setIsSelected(0);
            } else {
                mList.get(postion).setIsSelected(1);
            }
        }

        mAdapter.notifyDataSetChanged();
    }


    public void  getGroupList()
    {

        String authtoken = SharedPreferencesUtils.getSharedPreferences(
                Constants.CONFIG_AUTHTOKEN, getContext());
        HashMap<String,Object> params = new HashMap<String, Object>();
        params.put("Authorization", authtoken);
        mPresenter.setType(mType);
        mPresenter.getGoupList(params);

    }


    public void saveGroup()
    {

        String authtoken = SharedPreferencesUtils.getSharedPreferences(
                Constants.CONFIG_AUTHTOKEN, getContext());
        HashMap<String,Object> params = new HashMap<String, Object>();
        params.put("Authorization", authtoken);
        params.put("name", groupNameEt.getText().toString());
        mAddNoticeGroupPresenter.setType(mType);
        mAddNoticeGroupPresenter.addNoticeGroup(params);


    }

    public  void addRelGroup()
    {
        StringBuffer sb = new StringBuffer();
        for(int i=0;i<mChoooseGooupList.size();i++)
        {
            sb.append(mChoooseGooupList.get(i).getUuid());
            if(i<mChoooseGooupList.size() -1)
            {
                sb.append(";");
            }

        }

        String authtoken = SharedPreferencesUtils.getSharedPreferences(
                Constants.CONFIG_AUTHTOKEN, getContext());
        HashMap<String,Object> params = new HashMap<String, Object>();
        params.put("Authorization", authtoken);
        params.put("uuid", noticeUuid);

        if(!TextUtils.isEmpty(sb.toString()))
        {
            params.put("groupUuid", sb.toString());
        }
        mAddNoticeRelGroupPresenter.setType(mType);
        mAddNoticeRelGroupPresenter.addNoticeRelGroup(params);



    }


    @Override
    public void onProgress() {

    }

    @Override
    public void onError(Object obj) {

    }

    @Override
    public void onSuccess(Object obj) {

    }

    @Override
    public void getGroupListSuccess(final Object obj) {

        if(null != obj)
        {
            ArrayList<NoticeGroup> list  =(ArrayList<NoticeGroup>) obj;
            mList.addAll(list);
        }

        if(pageType ==2)
        {
            if(!TextUtils.isEmpty(gUuid))
            {
                if(gUuid.contains(";"))
                {
                    String[]  idArray =  gUuid.split(";");
                    for(int i=0;i<idArray.length;i++)
                    {
                        for(NoticeGroup gp :mList)
                        {
                            if(idArray[i].equals(gp.getUuid()))
                            {
                                gp.setIsSelected(1);
                            }
                        }

                    }

                }
                else
                {
                    //不包含封号
                    for(NoticeGroup gp :mList)
                    {
                        if(gp.getUuid().equals(gUuid))
                        {
                            gp.setIsSelected(1);
                        }
                    }
                }



            }
        }
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void getNoticeListSuccess(Object obj) {

    }

    @Override
    public void getNotificationUserSuccess(Object obj) {

    }

    @Override
    public void getNotificationRelGroupList(Object obj) {

    }

    @Override
    public void getNoticeRelGroupMemberListSuccess(Object obj) {

    }

    @Override
    public void addNoticeSBDXSuccess(String noticeUuid) {

    }

    @Override
    public void addSBSDSuccess(Object obj) {

    }

    @Override
    public void onGetDeviceLocListSuccess(Object obj) {

    }

    @Override
    public void onGetDeviceListSuccess(Object obj) {

    }

    @Override
    public void onGetNoticeDeviceGroupListSuccess(Object obj) {

    }

    @Override
    public void onGetNoticeDeviceGroupUuidSuccess(Object obj) {

    }

    @Override
    public void addNoticeDeviceSuccess(Object obj) {

    }

    @Override
    public void getTopGroupListSuccess(Object obj) {

    }

    @Override
    public void getNoticeTargetListSuccess(Object obj) {

    }

    @Override
    public void onGetNoticeTargetRelGroupListSuccess(Object obj) {

    }

    @Override
    public void onGetNoticeTargetGroupUuidSuccess(Object obj) {

    }

    @Override
    public void addNoticeTarGetDeviceSuccess(Object obj) {

    }

    @Override
    public void addNoticeDataSuccess(Object obj) {

    }

    @Override
    public void deleteNoticeSuccess(Object obj) {

    }

    @Override
    public void addNoticeGroupSuccess(Object obj) {

        //添加分组成功
        mList.clear();
        mAdapter.notifyDataSetChanged();

        model = 0;
        titleName.setText("选择分组");
        addTitle.setVisibility(View.VISIBLE);
        choosecontentRl.setVisibility(View.VISIBLE);
        addcontentRl.setVisibility(View.GONE);
        getGroupList();
    }

    @Override
    public void addNoticeRelGroupSuccess(Object obj) {


        //走返回流程
        myListener.refreshData("");
        dismiss();


    }

    @Override
    public void setNoticeFromSuccess(Object obj) {

    }

    @Override
    public void getNoticeDetailSuccess(Object obj) {

    }

    @Override
    public void deleteNoticeGroupSuccess(Object obj) {

    }

    @Override
    public void setPresenter(BasePresenter tp) {

    }



}
